﻿using System;
using System.Net;
using System.Net.Http;

namespace KD.Service.HR.Utility
{
   public class RequestToApi
    {
        /// <summary>
        /// Get类型请求
        /// </summary>
        /// <param name="service">服务模块</param>
        /// <param name="path">请求的api地址，例如：api/product/1</param>
        /// <returns>返回json数据</returns>
        public static HttpResponseModel Get(ApiService service, string path)
        {
            using (var client = new HttpClient())
            {
                var host = DBConfig.GetServiceHost(service);
                if (string.IsNullOrEmpty(host))
                {
                    throw new Exception("未配置服务主机地址！");
                }
                client.BaseAddress = new Uri(string.Format("http://{0}/", host));
                var response = client.GetAsync(path).Result;
                var content = response.Content.ReadAsStringAsync().Result;
                var statusCode = response.StatusCode;
                return new HttpResponseModel
                {
                    StatusCode = statusCode,
                    Content = content
                };
            }
        }

        /// <summary>
        /// Get类型请求
        /// </summary>
        /// <param name="url">请求的api地址</param>
        /// <returns>返回json数据</returns>
        public static HttpResponseModel Get(string url)
        {
            using (var client = new HttpClient())
            {
                var response = client.GetAsync(url).Result;
                var content = response.Content.ReadAsStringAsync().Result;
                var statusCode = response.StatusCode;
                return new HttpResponseModel
                {
                    StatusCode = statusCode,
                    Content = content
                };
            }
        }

        /// <summary>
        /// Post类型请求
        /// </summary>
        /// <param name="service">服务模块</param>
        /// <param name="path">请求的api地址</param>
        /// <param name="data">发送的数据</param>
        /// <returns>返回json数据</returns>
        public static HttpResponseModel Post(ApiService service, string path, object data)
        {
            using (var client = new HttpClient())
            {
                var host = DBConfig.GetServiceHost(service);
                if (string.IsNullOrEmpty(host))
                {
                    throw new Exception("未配置服务主机地址！");
                }
                client.BaseAddress = new Uri(string.Format("http://{0}/", host));
                var response = client.PostAsJsonAsync(path, data).Result;
                var content = response.Content.ReadAsStringAsync().Result;
                var statusCode = response.StatusCode;
                return new HttpResponseModel
                {
                    StatusCode = statusCode,
                    Content = content
                };
            }
        }

        /// <summary>
        /// Put类型请求
        /// </summary>
        /// <param name="service">服务模块</param>
        /// <param name="path">请求的api地址</param>
        /// <param name="data">发送的数据</param>
        /// <returns>返回json数据</returns>
        public static HttpResponseModel Put(ApiService service, string path, object data)
        {
            using (var client = new HttpClient())
            {
                var host = DBConfig.GetServiceHost(service);
                if (string.IsNullOrEmpty(host))
                {
                    throw new Exception("未配置服务主机地址！");
                }
                client.BaseAddress = new Uri(string.Format("http://{0}/", host));
                var response = client.PutAsJsonAsync(path, data).Result;
                var content = response.Content.ReadAsStringAsync().Result;
                var statusCode = response.StatusCode;
                return new HttpResponseModel
                {
                    StatusCode = statusCode,
                    Content = content
                };
            }
        }

        /// <summary>
        /// Delete类型请求
        /// </summary>
        /// <param name="service">服务模块</param>
        /// <param name="path">请求的api地址</param>
        /// <returns>返回json数据</returns>
        public static HttpResponseModel Delete(ApiService service, string path)
        {
            using (var client = new HttpClient())
            {
                var host = DBConfig.GetServiceHost(service);
                if (string.IsNullOrEmpty(host))
                {
                    throw new Exception("未配置服务主机地址！");
                }
                client.BaseAddress = new Uri(string.Format("http://{0}/", host));
                var response = client.DeleteAsync(path).Result;
                var content = response.Content.ReadAsStringAsync().Result;
                var statusCode = response.StatusCode;
                return new HttpResponseModel
                {
                    StatusCode = statusCode,
                    Content = content
                };
            }
        }
    }
    public class HttpResponseModel
    {
        public HttpStatusCode StatusCode { get; set; }
        public string Content { get; set; }
    }
}
